package com.zy.smbms.controller;

import com.zy.smbms.pojo.SmbmsBill;
import com.zy.smbms.service.SmbmsBillService;
import com.zy.smbms.utils.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("billController")
public class BillController
{
    //账单管理
    @Autowired
    private SmbmsBillService smbmsBillService;

    @RequestMapping("billlist")
    public String BillList(HttpSession session, String productName, String providerId, String flag, String pageNo)
    {
        SmbmsBill bill=new SmbmsBill();
        if("".equals(productName))
            productName=null;
        if("".equals(providerId))
            providerId=null;
        if("".equals(flag))
            flag=null;
        bill.setProductname(productName);
        if(providerId!=null)
            bill.setProviderid(Integer.parseInt(providerId));
        if(flag!=null)
            bill.setIspayment(Integer.parseInt(flag));
        Map<String,Object> map=new HashMap<>();
        int pageNon,pageSize,totalPageCount;
        if(pageNo==null)
        {
            pageNo="1";
        }
        pageNon=Integer.parseInt(pageNo);
        pageSize=3;
        PageSupport page=new PageSupport();
        page.setCurrentPageNo(pageNon);
        int count=smbmsBillService.selectTotalCount(bill);
        page.setPageSize(pageSize);
        page.setTotalCount(count);
        totalPageCount=page.getTotalPageCount();
        if(pageNon<1)
            pageNon=1;
        if(pageNon>totalPageCount)
            pageNon=totalPageCount;
        int[] num={(pageNon-1)*pageSize,pageSize};
        map.put("bill",bill);
        map.put("num",num);
        List<SmbmsBill> billList = smbmsBillService.selectiveBillList(map);
        session.setAttribute("billList",billList);
        session.setAttribute("bill",bill);
        session.setAttribute("page3",page);
        return "billList";
    }

    @RequestMapping("queryBill")
    @ResponseBody
    public String queryBill(HttpSession session,Long id)
    {
        SmbmsBill smbmsBill = smbmsBillService.queryBillById(id);
        session.setAttribute("smbmsBill",smbmsBill);
        return "OK";
    }

    @RequestMapping("BillView")
    public String BillView()
    {
        return "billView";
    }

    @RequestMapping("BillUpdate")
    public String BillUpdate()
    {
        return "billUpdate";
    }

    @RequestMapping("UpdateBill")
    @ResponseBody
    public String UpdateBill(SmbmsBill bill)
    {
        int i = smbmsBillService.updateBillById(bill);
        if(i>0)
            return "OK";
        else
            return "False";
    }

    //增加账单信息
    @RequestMapping("billAdd")
    public String billAddPage()
    {
        return "billAdd";
    }

    @ResponseBody
    @RequestMapping("AddBill")
    public String AddBill(SmbmsBill bill)
    {
        int i=smbmsBillService.addBill(bill);
        if(i>0)
            return "OK";
        else
            return "False";
    }

    @ResponseBody
    @RequestMapping("deleteBillById")
    public String deleteBillById(String id)
    {
        int i=smbmsBillService.deleteBillById(Long.parseLong(id));
        if(i>0)
            return "OK";
        else
            return "False";
    }
}
